package com.bjtech.web.action.management;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ssi.web.struts.bean.UploadFile;

import com.bjtech.dao.BaseBuildingInfoDao;
import com.bjtech.dao.BaseProjectInfoDao;
import com.bjtech.dao.HeatMeterInfoDao;
import com.bjtech.dao.SysAccessaryDao;
import com.bjtech.domain.BaseBuildingInfo;
import com.bjtech.domain.BaseProjectInfo;
import com.bjtech.domain.HeatMeterInfo;
import com.bjtech.domain.SysAccessary;
import com.bjtech.domain.query.BaseBuildingInfoQuerySpec;
import com.bjtech.domain.query.BaseShareAreaInfoQuerySpec;
import com.bjtech.domain.query.HeatMeterInfoQuerySpec;
import com.bjtech.web.action.LyheatBaseAction;

public class HeatMeterInfoAction extends LyheatBaseAction {

	private static final long serialVersionUID = 1L;
	private HeatMeterInfoDao heatMeterInfoDao;
	private BaseBuildingInfoDao baseBuildingInfoDao;
	private HeatMeterInfoQuerySpec selectBean;
	
	private SysAccessaryDao sysAccessaryDao;
	private SysAccessary accessary;
	
	private List buidList;
	
	private BaseProjectInfoDao baseProjectInfoDao;
	private List projList;
	
	private Map tjParam = new HashMap();
	
	public void prepare() throws Exception {

	}
	
	public HeatMeterInfoAction() throws Exception{
		model = new HeatMeterInfo();
		selectBean = new HeatMeterInfoQuerySpec();
	}
	
	public void setHeatMeterInfoDao(HeatMeterInfoDao heatMeterInfoDao) {
		this.heatMeterInfoDao = heatMeterInfoDao;
	}

	public String execute(){
		return this.list();
	}
	
	public String list(){
		buidList = baseBuildingInfoDao.selectEntityList(new BaseBuildingInfo(), new BaseBuildingInfoQuerySpec());
		projList = baseProjectInfoDao.selectEntityList(new BaseProjectInfo(), new BaseShareAreaInfoQuerySpec());
		selectBean.queryParamString();
		list = this.heatMeterInfoDao.selectEntityPaginatedList(new HeatMeterInfo(),selectBean);
		return "list";
	}
	
	public String add(){
		buidList = baseBuildingInfoDao.selectEntityList(new BaseBuildingInfo(), new BaseBuildingInfoQuerySpec());
		return "edit";
	}
	
	public String save(){
		HeatMeterInfo entity = (HeatMeterInfo)model;
		BaseBuildingInfo build = baseBuildingInfoDao.selectEntity(new BaseBuildingInfo(), entity.getBuild_id());
		entity.setArea_id(build.getArea_id());
		Long link_id = null;
		if(entity.getId()==null){
			Long count = heatMeterInfoDao.selectheatMetaInfoByMeterNum(entity.getMeter_num());
			if(count>=1){
				super.renderJavaScript("window.onload=function(){alert('热量表表号已存在，请您核对后重新输入！');location.href='HeatMeterInfo!add.action';}");
				return null;
			}
			link_id = heatMeterInfoDao.insertEntity(entity);
		}else{
			heatMeterInfoDao.updateEntity(entity);
			link_id = entity.getId();
		}
		
		SysAccessary sysAccessary = new SysAccessary();
		
		if(getUploadFileList().size()>0){//说明本次操作有新文件上传，此时就先把旧文件删除，再添加新文件
			this.sysAccessaryDao.deleteAccByLinkId(link_id);
		}
		for (UploadFile uploadFile : getUploadFileList()) {
			sysAccessary = new SysAccessary();
			sysAccessary.setLink_id(link_id);
			sysAccessary.setLink_tab("P_HEAT_METER_INFO");
			sysAccessary.setAc_sname(uploadFile.getFileName());
			sysAccessary.setUrl(uploadFile.getFileSavePath());
			this.sysAccessaryDao.insertEntity(sysAccessary);
		}
		return "success";
	}
	
	public String edit(){
		buidList = baseBuildingInfoDao.selectEntityList(new BaseBuildingInfo(), new BaseBuildingInfoQuerySpec());
		model = this.heatMeterInfoDao.selectEntity(new HeatMeterInfo(),id);
		//查询附件
		accessary = this.sysAccessaryDao.selectAccListByLinkId(id);
		return "edit";
	}

	public String view(){
		model = this.heatMeterInfoDao.selectEntity(new HeatMeterInfo(),id);
		//查询附件
		accessary = this.sysAccessaryDao.selectAccListByLinkId(id);
		
		tjParam = this.heatMeterInfoDao.selectTjParamForHeatMeter(id);
		return "view";
	}
	
	public String delete(){
		model = this.heatMeterInfoDao.deleteEntity(new HeatMeterInfo(),id);
		return "success";
	}
	
	public HeatMeterInfoQuerySpec getSelectBean() {
		return selectBean;
	}

	public void setSelectBean(HeatMeterInfoQuerySpec selectBean) {
		this.selectBean = selectBean;
	}

	public BaseBuildingInfoDao getBaseBuildingInfoDao() {
		return baseBuildingInfoDao;
	}

	public void setBaseBuildingInfoDao(BaseBuildingInfoDao baseBuildingInfoDao) {
		this.baseBuildingInfoDao = baseBuildingInfoDao;
	}

	public List getBuidList() {
		return buidList;
	}

	public void setBuidList(List buidList) {
		this.buidList = buidList;
	}

	public HeatMeterInfoDao getHeatMeterInfoDao() {
		return heatMeterInfoDao;
	}

	public SysAccessaryDao getSysAccessaryDao() {
		return sysAccessaryDao;
	}

	public void setSysAccessaryDao(SysAccessaryDao sysAccessaryDao) {
		this.sysAccessaryDao = sysAccessaryDao;
	}

	public SysAccessary getAccessary() {
		return accessary;
	}

	public void setAccessary(SysAccessary accessary) {
		this.accessary = accessary;
	}

	public Map getTjParam() {
		return tjParam;
	}

	public void setTjParam(Map tjParam) {
		this.tjParam = tjParam;
	}

	public BaseProjectInfoDao getBaseProjectInfoDao() {
		return baseProjectInfoDao;
	}

	public void setBaseProjectInfoDao(BaseProjectInfoDao baseProjectInfoDao) {
		this.baseProjectInfoDao = baseProjectInfoDao;
	}

	public List getProjList() {
		return projList;
	}

	public void setProjList(List projList) {
		this.projList = projList;
	}
}
